<?php

class Dcmp_Component_Component {
	
	private $_domain = NULL;

    public function __construct($domain) {
    	$this->_domain = $domain;
    }
    
    public function getDomain(){
    	return $this->_domain;
    }
    
    public function loadController($name = NULL) {
    	$router = Kd_Web::getRouter();
    	if(is_null($name)){
    		$name = $router->getController();
    	}

        $className = Kd_Loader::loadClass(implode('_', array(
	                    $this->getDomain()->getName(),
	                    'component',
	                    $router->getComponent(),
	                    'controller',
	                    $name)));
	    return new $className($this);
    }

    public function loadView($actionName = NULL, $controllerName = NULL) {
    	$router = Kd_Web::getRouter();
        if (is_null($actionName)) {
            $actionName = $router->getAction();
        }
        if (is_null($controllerName)) {
            $controllerName = $router->getController();
        }

        $viewName = str_replace('-', '_', $actionName);
        $className = Kd_Loader::loadClass(implode('_', array(
			        	$this->getDomain()->getName(), 
			        	'component', $router->getComponent(), 
			        	'view', 
			        	$controllerName, 
			        	$viewName)));
        return new $className($this);
    }     

    protected function initialize(){}
    
	protected function authenticate() {}

	protected function authorization(){}

	protected function executeAction(){
    	$actionResult = $this->loadController()->execute();
        if(is_null($actionResult)){
            $actionResult = new Dcmp_Component_Result_View($this);
        }
        $actionResult->execute();
	}

    protected function finish(){}
    
    public function run(){
    	$this->initialize();
    	$this->authenticate();
    	$this->authorization();
    	$this->executeAction();
    	$this->finish();
    }
    
    public function callAction(){
    	return NULL;
    }
}

